<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '300px'
        }
    },
    data() {
        return {
            chart: null 
        }
    },
    mounted() {
        
        let a = [];
        let b = [];
        let c = [];

        a = [ '2022-01-01' , '2022-01-02' , '2022-01-03' , '2022-01-04' , '2022-01-05' , '2022-01-06' , '2022-01-07' ,  ];
        b = [ '200','130','230','180','130','280','170'  ];
        c = [ '400','200','123','41','80', '110', '90'];

        this.initChart( a , b , c )

        // this.$api.getLatestOrderAmount().then( res=>{

        //     let a = [];
        //     let b = [];
        //     let c = [];

        //     console.log( res );
        //     for (let index = 0; index < res.data.length; index++) {
        //         const element =  res.data[index];
        //         a.push( element.date );
        //         b.push( element.amount );
        //         c.push( element.count );
        //     }
        //     console.log( a );
        //     console.log( b );
        //     console.log( c );
        //     this.initChart( a , b , c )
        // });


        this.$nextTick(() => {
            // this.initChart( a , b , c )
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart( a , b  , c ) {
            this.chart = echarts.init(this.$el, 'macarons')

            this.chart.setOption({
                tooltip: {
                trigger: 'axis',
                    axisPointer: { // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    top: 10,
                    left: '2%',
                    right: '2%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: a,
                    axisTick: {
                        alignWithLabel: true
                    }
                }],
                yAxis: [{
                    type: 'value',
                    axisTick: {
                        show: false
                    }
                }],
                series: [
                    {
                        name: '订单金额',
                        type: 'bar',
                        stack: 'vistors',
                        barWidth: '60%',
                        data: b,
                        animationDuration
                    },
                    {
                        name: '订单数量',
                        type: 'bar',
                        stack: 'vistors',
                        barWidth: '60%',
                        data: c,
                        animationDuration
                    }
                ]
            })
        }
    }
}
</script>
